package org.chnxi.study.redis;

import lombok.extern.slf4j.Slf4j;
import org.chnxi.study.redis.config.RedisOperator;
import org.chnxi.study.redis.lock.Lock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisCluster;

import java.io.Serializable;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@SpringBootApplication
public class RedisApplication {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    //private JedisCluster jedisCluster;
    private RedisOperator redisOperator;

    @Autowired
    Lock lock;

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(RedisApplication.class, args);

        RedisApplication application = context.getBean(RedisApplication.class);

//        String timestamp = "timestamp="+System.currentTimeMillis();
//
//        application.setString("cur" , timestamp);
//
//        String result = application.getString("cur");
//
//        System.out.println("--->>result="+result);

//        application.setLotdatas();
//        application.getLotDatas();

        application.jedisGetLotDatas();

//        application.randomKeys();


    }

    public void jedisGetLotDatas(){
        String prefix = "testkey@15977182";
        while (true){
            int idx = 0;
            while (idx < 10000) {
                idx = (int) (Math.random() * 100000);
                //System.out.println(idx);
            }
            int errTimes = 0;
            while (errTimes<3){
                try{
                    //redisOperator.get()
                    String result = (String)redisOperator.get(prefix+idx);//getString(prefix+idx);
                    //if(errTimes != 0){
                        System.out.println("errorTimes="+errTimes+";get String = "+result);
                    //}
                    break;
                }catch (Exception e){
                    errTimes++;
                    System.out.println(errTimes+"====>>>"+e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

    public void getLotDatas(){
        String prefix = "testkey@15977182";
        while (true){
            int idx = 0;
            while (idx < 10000) {
                idx = (int) (Math.random() * 100000);
                //System.out.println(idx);
            }
            int errTimes = 0;
            while (errTimes<3){
                try{
                    String result = getString(prefix+idx);
                    //System.out.println("get String = "+result);
                    break;
                }catch (Exception e){
                    errTimes++;
                    System.out.println(errTimes+"====>>>"+e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

    public void jedisSetLotDatas(){
        String prefix = "testkey@";
        long oldkey = 0L;
        while (true){
            long newkey = System.currentTimeMillis();
            if(oldkey == newkey){
                try {
                    TimeUnit.MILLISECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                continue;
            }
//            try {
//                TimeUnit.MILLISECONDS.sleep(200);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            oldkey = newkey;
            redisOperator.set(prefix+newkey , prefix+newkey+"=TestData123456789012345678901234567890");
            System.out.println(prefix+newkey+"=TestData123456789012345678901234567890");
        }

    }

    public void setLotdatas(){
        String prefix = "testkey@";
        long oldkey = 0L;
        while (true){
            long newkey = System.currentTimeMillis();
            if(oldkey == newkey){
                try {
                    TimeUnit.MILLISECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                continue;
            }
//            try {
//                TimeUnit.MILLISECONDS.sleep(200);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            oldkey = newkey;

            setString(prefix+newkey , prefix+newkey+"=TestData123456789012345678901234567890");
            System.out.println(prefix+newkey+"=TestData123456789012345678901234567890");
        }
    }

    public void randomKeys(){
        for (int i=0; i<100; i++){
            String key = UUID.randomUUID().toString();
            String val = System.currentTimeMillis()+"";
            redissonClient.getBucket(key).set(val);
            System.out.println("key="+key+"; val="+val);
        }
    }

    public void setString(String key, String value){
        redissonClient.getBucket(key).set(value);
    }

    public String getString(String key){
        return (String)redissonClient.getBucket(key).get();
    }

    public static int nums = 100;

    public void test(){
        for (int i=0; i<10; i++){
            new Thread(()->{
                while (nums>0){
                    try {
                        TimeUnit.MILLISECONDS.sleep(1l);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try{
                        if(lock.lock("ticketSall")){
                            try{

                                if(nums > 0){
                                    nums = nums -1;
                                    System.out.println(Thread.currentThread().getName()+":nums="+nums);
                                }


                                try {
                                    TimeUnit.SECONDS.sleep(11);
//                                    TimeUnit.MILLISECONDS.sleep(1);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                System.out.println(Thread.currentThread().getName()+":执行完成");

                            }catch (Exception e){
                                e.printStackTrace();
                            }finally {
                                lock.unlock("ticketSall");
                            }
                        }
                    }catch (Exception e){
                        System.out.println(Thread.currentThread().getName()+"========>>>"+e.getMessage());
                        log.error(e.getMessage() , e);
                    }
                }

            }, "Thread_"+i).start();
        }
    }

}
